﻿Imports System.Web
Imports System.Data
Imports System.Data.OleDb
Imports System.IO
Imports System.Web.UI.WebControls


Public Class routeinfo
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'get and display visitor name on page instead of allowing visitor to type in name
        If (Application("AnonCount") IsNot Nothing) Then
            lbl_routecommentname.Text = Request.AnonymousID
        End If

        If (User.Identity.IsAuthenticated) Then
            lbl_routecommentname.Text = User.Identity.Name
            tb_routecommentemail.Text = Membership.GetUser().Email
        Else
            lbl_routecommentname.Text = Request.AnonymousID
        End If

        'display buttons depending on visitor status
        If (User.Identity.IsAuthenticated) Then
            btn_rateroute.Visible = True
            btn_routecommentadd.Visible = True
        Else
            btn_rateroute.Visible = False
            btn_routecommentadd.Visible = False
        End If

    End Sub

    Protected Sub FormView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewPageEventArgs) Handles FormView1.PageIndexChanging

    End Sub

    Protected Sub linkb_routedetails_Click(ByVal sender As Object, ByVal e As EventArgs)
        Response.ClearHeaders()
        Response.ContentType = "application/octet-stream"
        Response.AppendHeader("content-disposition", String.Format("attachment;filename={0}", Path.GetFileName("routedetails")))
        Response.WriteFile("routedetails")
        Response.Flush()
        Response.End()
    End Sub

    Protected Sub btn_rateroute_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_rateroute.Click
        'create db connection
        Dim OleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("walkingweb").ConnectionString)
        OleDbConn.Open()
        Dim SqlString As String = "Insert into routevisitorrating  (RouteID_fk,Visitor,RouteRate) Values (@f1,@f2,@f3)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, OleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", Request.Params("RouteID"))
        cmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f3", ajc_rateroute.CurrentRating)
        cmd.ExecuteNonQuery()
    End Sub

    Protected Sub btn_routecommentadd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_routecommentadd.Click
        Dim OleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("walkingweb").ConnectionString)
        Dim SqlString As String = "Insert into routecomments (Visitor,Email,WebsiteURL,Title,Content,DatePosted,RouteID_fk) Values (@f1,@f2,@f3,@f4,@f5,@f6,@f7)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, OleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", lbl_routecommentname.Text)
        cmd.Parameters.AddWithValue("@f2", tb_routecommentemail.Text)
        cmd.Parameters.AddWithValue("@f3", tb_routecommenturl.Text)
        cmd.Parameters.AddWithValue("@f4", tb_routecommenttitle.Text)
        cmd.Parameters.AddWithValue("@f5", tb_routecommentcontent.Text)
        cmd.Parameters.AddWithValue("@f6", Date.Today)
        cmd.Parameters.AddWithValue("@f7", Request.Params("RouteID"))
        OleDbConn.Open()
        cmd.ExecuteNonQuery()
        Response.Redirect("confirmationcomment.aspx")
    End Sub

    Protected Sub btn_routetagadd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_routetagadd.Click
        'create db connection
        Dim OleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("walkingweb").ConnectionString)
        Dim SqlString As String = "Insert into routetagtable (RouteID_fk,TagID_fk) Values (@f1,@f2)"
        OleDbConn.Open()

        'create checkboxlist to display existing tags for item
        For Each cb As ListItem In cbl_addexistingtag.Items
            If cb.Selected Then

                ' here is where we add the join to the table of tags and posts
                Dim cmd As OleDbCommand = New OleDbCommand(SqlString, OleDbConn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@f1", Request.Params("RouteID"))
                cmd.Parameters.AddWithValue("@f2", cb.Value)
                cmd.ExecuteNonQuery()
            End If
        Next

        'adding new tag to item
        If tb_newroutetags.Text = " " Then Return
        Dim newTags = tb_newroutetags.Text.Split(",")
        For Each newTag In newTags
            'first add the new tag to the tag table
            Dim newTagSql As String = "Insert INTO tags(TagText) VALUES(@f1)"
            Dim cmd As OleDbCommand = New OleDbCommand(newTagSql, OleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", newTag)
            cmd.ExecuteNonQuery()

            'then find the id of the new tag
            Dim getLastIDCmd As OleDbCommand = New OleDbCommand("Select TOP 1 TagID from tags Order by TagID Desc", OleDbConn)
            Dim dr = getLastIDCmd.ExecuteReader()
            dr.Read()
            Dim lastID = dr.GetValue(0)

            'associate this last ID with Item
            Dim newcmd As OleDbCommand = New OleDbCommand(SqlString, OleDbConn)
            newcmd.CommandType = CommandType.Text
            newcmd.Parameters.AddWithValue("@f1", Request.Params("RouteID"))
            newcmd.Parameters.AddWithValue("@f2", lastID)
            newcmd.ExecuteNonQuery()

        Next

    End Sub
End Class